package cn.lystudio.rohmtimeopwide_background_v11.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.SecDeptDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.TopDeptDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.DepartmentsPO;
import cn.lystudio.rohmtimeopwide_background_v11.mapper.DepartmentsPOMapper;
import cn.lystudio.rohmtimeopwide_background_v11.service.DepartmentService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
@DS("master")
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentsPOMapper departmentsPOMapper;

    /**
     * 鉴权,获取所有的制造部信息(包含enabled)
     * @return
     */
    @Override
    public List<TopDeptDTO> getAll4TopDept() {
        List<String> topDeptPermisList = StpUtil.getPermissionList(StpUtil.getLoginIdAsString());
        if(topDeptPermisList.size()!=1) return null;
        LambdaQueryWrapper<DepartmentsPO> wrapper = new QueryWrapper<DepartmentsPO>().lambda();
        wrapper//.eq(DepartmentsPO::getEnabled,true)
                .eq(DepartmentsPO::getParentID,0)   //父ID=0
                .ge(DepartmentsPO::getDepartmentID,10)      //自己的ID>10
                // 非超级管理员
                .eq(Convert.toInt(topDeptPermisList.get(0))>=10,DepartmentsPO::getDepartmentID,topDeptPermisList.get(0));
        List<DepartmentsPO> departmentsList = departmentsPOMapper.selectList(wrapper);
        List<TopDeptDTO> topDeptList = CollUtil.list(false);
        CollUtil.forEach(departmentsList.iterator(),(department, index) ->{
            TopDeptDTO topDept = new TopDeptDTO();
            topDept.setTopDeptID(department.getDepartmentID());
            topDept.setTopDeptName(department.getDepartmentName());
            topDept.setEnabled(department.getEnabled());
            topDeptList.add(topDept);
        } );
        return topDeptList;
    }

    @Override
    public int uptDeptEnabled(Integer departmentID, Boolean enabled) {
        DepartmentsPO departmentsPO = new DepartmentsPO();
        departmentsPO.setDepartmentID(departmentID);
        departmentsPO.setEnabled(enabled);
        return departmentsPOMapper.updateById(departmentsPO);
    }

    @Override
    public int delDept(Integer departmentID) {
        return departmentsPOMapper.deleteById(departmentID);
    }

    @Override
    public int addDept(DepartmentsPO departmentsPO) {
        return departmentsPOMapper.insert(departmentsPO);
    }

    @Override
    public int uptDept(DepartmentsPO departmentsPO) {
        return departmentsPOMapper.updateById(departmentsPO);
    }

    /**
     * 鉴权,获取所有的制造工程信息(包含enabled)
     * @return
     */
    @Override
    public List<SecDeptDTO> getAll4SecDept() {
        // 1.获取制造部级别权限LIST
        List<String> topDeptPermisList = StpUtil.getPermissionList(StpUtil.getLoginIdAsString());
        if(topDeptPermisList.size()!=1) return null;
        LambdaQueryWrapper<DepartmentsPO> wrapper = new QueryWrapper<DepartmentsPO>().lambda();
        wrapper.eq(DepartmentsPO::getEnabled,true)      //enabled=true
                .eq(DepartmentsPO::getParentID,0)       // 父ID=0
                .ge(DepartmentsPO::getDepartmentID,10)     // 自己ID>10
                // 非超级管理员
                .eq(Convert.toInt(topDeptPermisList.get(0))>=10,DepartmentsPO::getDepartmentID,topDeptPermisList.get(0));
        List<DepartmentsPO> topDeptList = departmentsPOMapper.selectList(wrapper);
        if(CollUtil.isEmpty(topDeptList)) return null;
        // 1.1 将制造部权限转换成ID List
        List<Integer> topDeptIDList = CollUtil.list(false);
        CollUtil.forEach(topDeptList.iterator(),(topDept,index)->{
            topDeptIDList.add(topDept.getDepartmentID());
        });
        // 2. 通过制造部权限ID List,筛选出可以使用的制造工程List
        LambdaQueryWrapper<DepartmentsPO> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(DepartmentsPO::getParentID,topDeptIDList)       //父ID在TopID List中
                .ne(DepartmentsPO::getParentID,0)   //父ID <>0
                .ge(DepartmentsPO::getDepartmentID,10);  //自己ID>10
        List<DepartmentsPO> secDeptList=departmentsPOMapper.selectList(wrapper1);
        // 3.组合成最终数据
        List<SecDeptDTO> secDeptDTOList = CollUtil.list(false);
        CollUtil.forEach(secDeptList.iterator(),(secDept, index) ->{
            SecDeptDTO secDeptDTO = new SecDeptDTO();
            secDeptDTO.setSecDeptID(secDept.getDepartmentID());
            secDeptDTO.setSecDeptName(secDept.getDepartmentName());
            secDeptDTO.setLocation4SecD(secDept.getLocation4SecD());
            secDeptDTO.setEnabled(secDept.getEnabled());
            secDeptDTO.setBelong2TopID(secDept.getParentID());
            secDeptDTO.setResponsibleName(secDept.getResponsibleName());
            secDeptDTO.setTwiceOutput(secDept.getTwiceOutput());
            secDeptDTO.setTopDepartment(departmentsPOMapper.selectOne(new LambdaQueryWrapper<DepartmentsPO>().eq(DepartmentsPO::getDepartmentID,secDept.getParentID()).eq(DepartmentsPO::getEnabled,true)));
            secDeptDTOList.add(secDeptDTO);
        });
        // 3.2 过滤掉无用数据
        CollUtil.filter(secDeptDTOList,secDeptDTO -> ObjectUtil.isNotEmpty(secDeptDTO.getTopDepartment()));
        return secDeptDTOList;
    }

    @Override
    public List<DepartmentsPO> getAllSecDeptByTopDeptID(Integer topDeptID) {
        LambdaQueryWrapper<DepartmentsPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(DepartmentsPO::getParentID,0)
                .eq(DepartmentsPO::getParentID,topDeptID)
                .eq(DepartmentsPO::getEnabled,true);
        return departmentsPOMapper.selectList(wrapper);
    }

    @Override
    public DepartmentsPO getDeptByID(Integer departmentID) {
        LambdaQueryWrapper<DepartmentsPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DepartmentsPO::getDepartmentID,departmentID);
        return departmentsPOMapper.selectOne(wrapper);
    }


}
